package com.eqdd.yiqidian.ui.mine.qiye;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.ComInvitedActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.annotation.DealCode;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.CodeRule;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.InvitedListResultBean;
import com.eqdd.yiqidian.helper.mvchelper.ModelRx2DataSource;
import com.eqdd.yiqidian.helper.mvchelper.Rx2DataSource;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonConverter;
import com.eqdd.yiqidian.rxbus2.Subscribe;
import com.eqdd.yiqidian.rxbus2.ThreadMode;
import com.lzy.okgo.OkGo;
import com.lzy.okrx2.adapter.FlowableBody;
import com.shizhefei.mvc.MVCSwipeRefreshHelper;

import org.reactivestreams.Publisher;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

@DealCode(codes = {CodeRule.STAFF_RUZHI})
public class ComInvitedActivity extends BaseActivity {
    private ComInvitedActivityCustom dataBinding;
    private SlimAdapterEx<InvitedListResultBean> slimAdapterEx;
    private ArrayList<InvitedListResultBean> datas;
    private MVCSwipeRefreshHelper<List<InvitedListResultBean>> mvcHelper;
    private ModelRx2DataSource<InvitedListResultBean> dataSource;

    @Subscribe(codes = {CodeRule.STAFF_RUZHI}, threadMode = ThreadMode.MAIN)
    public void refreshCode() {

    }

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_com_invited);
        initTopTitleBar(View.VISIBLE, "入职邀请");
    }

    @Override
    public void initData() {
        isRxbus = true;
        datas = new ArrayList<>();
    }

    @Override
    public void setView() {
        if (slimAdapterEx == null) {
            dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
            slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_06, new SlimInjector<InvitedListResultBean>() {
                @Override
                public void onInject(InvitedListResultBean data, IViewInjector injector) {
                    injector.text(R.id.tv_upper_content, data.getCompany())
                            .visibility(R.id.checkbox, View.GONE)
                            .text(R.id.tv_down_content, "入职部门：" + data.getUdepartment() + "  入职岗位：" + data.getUpost());
                }
            }).attachTo(dataBinding.recyclerView);
            ItemClickSupport.addTo(dataBinding.recyclerView).setOnItemClickListener((rv, position, v) -> {
                Bundle bundle = new Bundle();
                bundle.putParcelable(Config.COM_INVITE_INFO, slimAdapterEx.getData().get(position));
                startActivityForResult(ComInviteInfoActivity.class, bundle, Config.AC_COM_INVITE_INFO);
            });
        }
        slimAdapterEx.updateData(datas);
        mvcHelper = new MVCSwipeRefreshHelper<>(dataBinding.swipeRefresh);
        dataSource = new ModelRx2DataSource<>(new ModelRx2DataSource.OnLoadSource() {
            @Override
            public Flowable<List> loadSource(int page, Rx2DataSource.DoneActionRegister register) {
                return OkGo.<HttpResult<List<InvitedListResultBean>>>post(HttpConfig.BASE_URL + HttpConfig.GET_COM_INVITE_LIST)
                        .params("user", ComInvitedActivity.this.user.getUname())
                        .converter(new JsonConverter<HttpResult<List<InvitedListResultBean>>>() {
                            @Override
                            public void test() {
                                super.test();
                            }

                        })
                        .adapt(new FlowableBody<HttpResult<List<InvitedListResultBean>>>())
                        .flatMap(new Function<HttpResult<List<InvitedListResultBean>>, Publisher<List<InvitedListResultBean>>>() {
                            @Override
                            public Publisher<List<InvitedListResultBean>> apply(@NonNull HttpResult<List<InvitedListResultBean>> listHttpResult) throws Exception {
                                return Flowable.just(listHttpResult.getItems() == null ? new ArrayList<InvitedListResultBean>() : listHttpResult.getItems());
                            }
                        });
            }
        });
        mvcHelper.setAdapter(slimAdapterEx);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.refresh();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS) {
            switch (requestCode) {
                case Config.AC_COM_INVITE_INFO:
                    mvcHelper.refresh();
                    break;
            }
        }
    }
}
